package com.cxh.mchat.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxh.mchat.bussiness.bean.UserContactsReqBean;
import com.cxh.mchat.common.constant.StatusConstant;
import com.cxh.mchat.common.security.bean.LoginUser;
import com.cxh.mchat.common.utils.SecurityUtils;
import com.cxh.mchat.user.bean.UserContactInfo;
import com.cxh.mchat.user.entity.User;
import com.cxh.mchat.user.entity.UserContacts;
import com.cxh.mchat.user.mapper.UserContactsMapper;
import com.cxh.mchat.user.service.IUserContactsService;
import com.cxh.mchat.user.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户联系人 服务实现类
 * </p>
 *
 * @author cxh
 * @since 2021-12-30
 */
@Service
public class UserContactsServiceImpl extends ServiceImpl<UserContactsMapper, UserContacts> implements IUserContactsService {

    @Resource
    private IUserService userService;

    @Override
    public UserContactInfo queryContactsInfo(String bussinssNo) {
        User bussinssUser = userService.selectByUserNo(bussinssNo);
        Assert.notNull(bussinssUser, "联系人信息不存在");

        UserContacts userContacts = getBaseMapper().selectOne(new QueryWrapper<UserContacts>()
                .eq("user_no", SecurityUtils.getLoginUser().getUserNo())
                .eq("bussiness_no", bussinssNo));
       if(null == userContacts){
           return null;
       }

        UserContactInfo userContactInfo = BeanUtil.copyProperties(userContacts, UserContactInfo.class);
        userContactInfo.setBussinssUser(bussinssUser);

        return userContactInfo;
    }

    @Override
    public List<UserContacts> query(UserContactsReqBean reqBean) {
        QueryWrapper<UserContacts> q = new QueryWrapper<UserContacts>().eq("user_no", SecurityUtils.getLoginUser().getUserNo());
        q.orderByDesc("create_time");
        if(StringUtils.hasText(reqBean.getBussinessName())){
            q.like("bussiness_name", reqBean.getBussinessName());
        }
        return getBaseMapper().selectList(q);
    }

    @Override
    public Boolean add(UserContacts reqBean) {
        Assert.hasText(reqBean.getBussinessNo(), "账号不能为空");
        User contacts = userService.selectByUserNo(reqBean.getBussinessNo());
        Assert.notNull(contacts, "账号不存在");

        UserContactInfo userContactInfo = queryContactsInfo(reqBean.getBussinessNo());
        Assert.isNull(userContactInfo, "你们已经是好友关系啦");

        LoginUser loginUser = SecurityUtils.getLoginUser();
        reqBean.setUserNo(loginUser.getUserNo());
        reqBean.setStatus(StatusConstant.TRUE);
        reqBean.setTopFlag(StatusConstant.FALSE);
        reqBean.setMuteFlag(StatusConstant.FALSE);
        reqBean.setBussinessName(contacts.getNickName());

        save(reqBean);

        //双方互为好友
        UserContacts userContacts = new UserContacts();
        userContacts.setUserNo(reqBean.getBussinessNo());
        userContacts.setBussinessNo(loginUser.getUserNo());
        userContacts.setBussinessName(loginUser.getUser().getNickName());
        userContacts.setStatus(StatusConstant.TRUE);
        userContacts.setTopFlag(StatusConstant.FALSE);
        userContacts.setMuteFlag(StatusConstant.FALSE);

        return save(userContacts);
    }

}
